package com.kwum.toutiao;

import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.kwum.TimeUtils;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

/**
 * @Description: 
 * @Company: yimo
 * @author: Kwum
 * @date 2018年2月12日 上午9:02:49
 */

public class ToutiaoArticleDemo {

	public static void main(String[] args) throws Exception {
//		String cookie = "sso_login_status=1; login_flag=15c2683787c7edecc93f177c9929c323; sessionid=dd1cf67c89cde4453f3bf3cafc182b8b; sid_guard=\"dd1cf67c89cde4453f3bf3cafc182b8b|1518073982|15552000|Tue\\\\054 07-Aug-2018 07:13:02 GMT\"; sid_tt=dd1cf67c89cde4453f3bf3cafc182b8b; _mp_auth_key=da5a887c83b3d4e283dee01ae0eaee4a; uid_tt=78cdb1aafc66165cf997f93e5d4b50b4";
        
		List<TouTiaoAccount> accountList = MySQLUtils.selectAccount();
		for(TouTiaoAccount a : accountList) {
			JSONArray articleList = getArticleList(a.getCookie(), null);
	        boolean result = getArticleInfo(a.getUsername(), a.getNickname(), "2018-02-08 00:00:00", articleList);
	        while(result){
	            articleList = getArticleList(a.getCookie(), articleList);
	            result = getArticleInfo(a.getUsername(), a.getNickname(), "2018-02-08 00:00:00", articleList);
	        }
		}
	}
	
	/**
     * 通过cookie获取文章列表
     * @param cookie cookie
     * @param articlesJson 第一页为空，其他则传入第一页返回的数据
     * @return
     * @throws Exception
     * @author kwum
     */
    public static JSONArray getArticleList(String cookie, JSONArray articlesJson) throws Exception{
    	
        //获取文章列表
        //第一页
        if(null == articlesJson){
            Document doc = Jsoup.connect("http://mp.toutiao.com/core/article/media_article_list/")
                                .header("Cookie", cookie)
                                .data("count", "20")
                                .data("source_type", "0")
                                .data("status", "all")
                                .data("from_time", "0")
                                .data("item_id", "")
                                .ignoreContentType(true)
                                .get();
            JSONObject htmlStr = JSONObject.fromObject(doc.body().text());
            articlesJson = htmlStr.getJSONObject("data").getJSONArray("articles");
//            System.out.println(articlesJson.toString());
            return articlesJson;
            
        }else{
            
            //不是第一页则取列表中最后一条数据作为参数进行翻页
            JSONObject j = articlesJson.getJSONObject(articlesJson.size()-1);
            Document doc = Jsoup.connect("http://mp.toutiao.com/core/article/media_article_list/")
                                .header("Cookie", cookie)
                                .data("count", "20")
                                .data("source_type", "0")
                                .data("status", "all")
                                .data("from_time",  j.getString("create_time"))
                                .data("item_id", j.getString("pgc_id"))
                                .ignoreContentType(true)
                                .get();
            JSONObject htmlStr = JSONObject.fromObject(doc.body().text());
            JSONArray aJson = htmlStr.getJSONObject("data").getJSONArray("articles");
            
            //再进行列表整合
            articlesJson.addAll(aJson);
//            System.out.println(articlesJson.toString());
            return articlesJson;
        }
    }
    
    public static boolean getArticleInfo(String phone, String nick, String time, JSONArray articlesJson) throws Exception{
    	boolean result = true;
    	
        //查找对应的文章
        for(int i = 0; i < articlesJson.size(); i++){
            JSONObject j = (JSONObject) articlesJson.get(i);
            
            if(j.getLong("create_time") > TimeUtils.string2Timestamp(time)){
                if(null != j.get("was_recommended") && j.getInt("was_recommended") == 1){
//                    System.out.println(new TouTiaoArticle(j.getString("pgc_id"), j.getString("title"), j.getInt("play_effective_count"), 
//                    		j.getInt("impression_count"), true, TimeUtils.timestamp2String(j.getLong("create_time"))).toString());
                    MySQLUtils.insertArticle(phone, nick, j.getString("pgc_id"), 
                    		j.getString("title"), 1, j.getInt("impression_count"), j.getInt("play_effective_count"), 
                    		TimeUtils.timestamp2String(j.getLong("create_time")));
                }else{
//                    System.out.println(new TouTiaoArticle(j.getString("pgc_id"), j.getString("title"), j.getInt("play_effective_count"), 
//                    		j.getInt("impression_count"), false, TimeUtils.timestamp2String(j.getLong("create_time"))).toString());
                	 MySQLUtils.insertArticle(phone, nick, j.getString("pgc_id"), 
                     		j.getString("title"), 0, j.optInt("impression_count", 0), j.optInt("play_effective_count", 0), 
                     		TimeUtils.timestamp2String(j.getLong("create_time")));
                }
            }else {
            	result = false;
            }
        }
		return result;
    }
}
